package com.example.performance.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * 压测配置模型
 * 包含完整的压测参数和场景配置
 */
public class LoadTestConfig {

    @JsonProperty("testName")
    private String testName; // 测试名称

    @JsonProperty("description")
    private String description; // 测试描述

    @JsonProperty("concurrentUsers")
    private int concurrentUsers = 1; // 并发用户数，默认1

    @JsonProperty("testDurationSeconds")
    private int testDurationSeconds = 60; // 测试持续时间（秒），默认60秒

    @JsonProperty("rampUpSeconds")
    private int rampUpSeconds = 0; // 预热时间（秒），默认0秒

    @JsonProperty("requestsPerSecond")
    private int requestsPerSecond = 0; // 每秒请求数限制，0表示无限制

    @JsonProperty("scenarios")
    private List<TestScenario> scenarios = new ArrayList<>(); // 测试场景列表

    @JsonProperty("globalHeaders")
    private java.util.Map<String, String> globalHeaders = new java.util.HashMap<>(); // 全局请求头

    @JsonProperty("reportFormat")
    private ReportFormat reportFormat = ReportFormat.CONSOLE; // 报告格式，默认控制台输出

    @JsonProperty("outputPath")
    private String outputPath = "./results"; // 报告输出路径

    /**
     * 报告格式枚举
     */
    public enum ReportFormat {
        @JsonProperty("console")
        CONSOLE, // 控制台输出
        @JsonProperty("json")
        JSON, // JSON格式
        @JsonProperty("html")
        HTML, // HTML格式
        @JsonProperty("csv")
        CSV // CSV格式
    }

    // 构造函数
    public LoadTestConfig() {
    }

    public LoadTestConfig(String testName) {
        this.testName = testName;
    }

    // Getter和Setter方法
    public String getTestName() {
        return testName;
    }

    public void setTestName(String testName) {
        this.testName = testName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getConcurrentUsers() {
        return concurrentUsers;
    }

    public void setConcurrentUsers(int concurrentUsers) {
        this.concurrentUsers = concurrentUsers;
    }

    public int getTestDurationSeconds() {
        return testDurationSeconds;
    }

    public void setTestDurationSeconds(int testDurationSeconds) {
        this.testDurationSeconds = testDurationSeconds;
    }

    public int getRampUpSeconds() {
        return rampUpSeconds;
    }

    public void setRampUpSeconds(int rampUpSeconds) {
        this.rampUpSeconds = rampUpSeconds;
    }

    public int getRequestsPerSecond() {
        return requestsPerSecond;
    }

    public void setRequestsPerSecond(int requestsPerSecond) {
        this.requestsPerSecond = requestsPerSecond;
    }

    public List<TestScenario> getScenarios() {
        return scenarios;
    }

    public void setScenarios(List<TestScenario> scenarios) {
        this.scenarios = scenarios;
    }

    public java.util.Map<String, String> getGlobalHeaders() {
        return globalHeaders;
    }

    public void setGlobalHeaders(java.util.Map<String, String> globalHeaders) {
        this.globalHeaders = globalHeaders;
    }

    public ReportFormat getReportFormat() {
        return reportFormat;
    }

    public void setReportFormat(ReportFormat reportFormat) {
        this.reportFormat = reportFormat;
    }

    public String getOutputPath() {
        return outputPath;
    }

    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
    }

    /**
     * 添加测试场景
     * 
     * @param scenario 测试场景对象
     */
    public void addScenario(TestScenario scenario) {
        this.scenarios.add(scenario);
    }

    /**
     * 验证配置有效性
     * 
     * @return 验证结果
     */
    @JsonIgnore
    public boolean isValid() {
        if (testName == null || testName.trim().isEmpty()) {
            return false;
        }
        if (concurrentUsers <= 0) {
            return false;
        }
        if (testDurationSeconds <= 0) {
            return false;
        }
        if (scenarios.isEmpty()) {
            return false;
        }
        // 验证每个场景是否有效
        for (TestScenario scenario : scenarios) {
            if (scenario.getRequests().isEmpty()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString() {
        return "LoadTestConfig{" +
                "testName='" + testName + '\'' +
                ", concurrentUsers=" + concurrentUsers +
                ", testDurationSeconds=" + testDurationSeconds +
                ", scenarioCount=" + scenarios.size() +
                '}';
    }
}